package common

import (
	"errors"
	"fmt"
	"gitee.com/dingyangzhuang/dingutils/dlog/logger"
	expect "github.com/google/goexpect"
	"time"

	"regexp"
	"strings"
)

var (
	userReg    *regexp.Regexp
	rootReg    *regexp.Regexp
	passwdReg  *regexp.Regexp
	storageReg *regexp.Regexp
	allReg     *regexp.Regexp
)

func init() {
	//创建正则表达式
	userReg, _ = regexp.Compile(`(.*\~\]\$ )|(.*\:\~\> )|(.*\]\$)|(.*\$.*)|(.*` + ADMIN_USER + `.*)`)
	rootReg, _ = regexp.Compile(`(.*#.*)|(.*root\>)`)
	passwdReg, _ = regexp.Compile(`.*(密码|Password|password).*`)
	//passwdReg, _ = regexp.Compile(`Password.*`)
	storageReg, _ = regexp.Compile(`.*liuyongqing:/>`)
	allReg, _ = regexp.Compile(`.*`)
}

/*
*
专门提取信息的，用于想server_detail表写入数据
*/
func UserDefineCmdSsh(e *expect.GExpect, argumentMap map[string]interface{}, addr string) error {
	isUbuntu := false
	isEuler := false
	//命令描述
	var field string
	cmdDescription := argumentMap["description"]
	if x, ok := argumentMap["field"]; !ok {
		field = ""
	} else {
		field = x.(string)
	}

	cmds := argumentMap[addr].([]string)
	var cmdMap map[string]string
	if x, ok := argumentMap["cmdMap"]; !ok {
		cmdMap = make(map[string]string, 0)
	} else {
		cmdMap = x.(map[string]string)
	}

	//要执行命令
	//cmd := argumentMap["cmd"]

	//sudouser := argumentMap["sudouser"]

	//判断是否为unbuntu的系统,因为ubuntu修改密码的命令是不一样的
	//logger.Info("%s 判断是否为ubuntu系统", addr)
	msg, err := sendRootMsg(e, "cat /proc/version |grep -i ubuntu |wc -l")

	if err != nil {
		logger.Error("%s 判断系统类型执行失败:%s", addr, err.Error())
		return errors.New(fmt.Sprintf("%s 判断系统类型执行失败:%s", addr, err.Error()))
	}
	if msg == "1" {
		isUbuntu = true
	}
	msg, err = sendRootMsg(e, "cat /proc/version |egrep \"oe[0-9]\"|wc -l")

	if err != nil {
		logger.Error("%s 判断系统类型执行失败:%s", addr, err.Error())
		return errors.New(fmt.Sprintf("%s 判断系统类型执行失败:%s", addr, err.Error()))
	}
	if msg == "1" || (msg != "1" && msg != "0") {
		isEuler = true
	}

	//修改root密码
	//logger.Info("%s 匹配成功,准备执行自定义命令", addr)
	if isUbuntu {
		//UpdateServerInfo(addr, "Ubuntu")
		for _, cmd := range cmds {
			if len(cmdMap) == 0 {
				msg, err = sendRootMsg(e, cmd)
				logger.Info("命令执行结果：", "ip", addr, "命令", cmd, "msg", msg)

			} else {
				//logger.Info("%s 执行 %s", addr, cmdMap["ubuntu"])
				msg, err = sendRootMsg(e, cmdMap["ubuntu"])
			}
			//查看是否存在sdb

			//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
			if field != "" {
				if err != nil {
					logger.Error("%s 命令：%s 失败:%s", addr, cmd, err.Error())
					return errors.New(fmt.Sprintf("%s 命令：%s 失败:%s", addr, cmd, err.Error()))
				}
				break
			}
			//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

			if err != nil {
				logger.Error("%s 命令：%s 失败:%s", addr, cmd, err.Error())
				return errors.New(fmt.Sprintf("%s 命令：%s 失败:%s", addr, cmd, err.Error()))
			}
		}
	} else {
		if isEuler {
			//logger.Info("isEuler")
			for _, cmd := range cmds {
				if len(cmdMap) == 0 {
					msg, err = sendRootMsgEuler(e, cmd)
					logger.Info("命令执行结果：", "ip", addr, "命令", cmd, "msg", msg)

				} else {
					msg, err = sendRootMsgEuler(e, cmdMap["euler"])
					logger.Info("%s 执行 %s", addr, cmdMap["euler"])
					//logger.Info("%s 执行 %s", addr, msg)

				}
				//查看是否存在sdb
				//logger.Info("执行命令：%s", cmd)
				//msg, err = sendRootMsgEuler(e, cmd)
				//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
				//fmt.Printf("%+v \n", msg)
				//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
				//msg = strings.ReplaceAll(msg, "\033", "")
				//msg = strings.ReplaceAll(msg, "[?2004l", "")
				//msg = strings.ReplaceAll(msg, "[01;31m[K", "")
				//msg = strings.ReplaceAll(msg, "[m[K", "")
				//msg = strings.ReplaceAll(msg, "\r", "")

				if field != "" {
					if err != nil {
						logger.Error("%s 命令：%s 失败:%s", addr, cmd, err.Error())
						return errors.New(fmt.Sprintf("%s 命令：%s 失败:%s", addr, cmd, err.Error()))
					}
					break
				}
				if err != nil {
					logger.Error("%s 命令：%s 失败:%s", addr, cmd, err.Error())
					return errors.New(fmt.Sprintf("%s 命令：%s 失败:%s", addr, cmd, err.Error()))
				}
			}
		} else {
			for _, cmd := range cmds {
				if len(cmdMap) == 0 {
					msg, err = sendRootMsg(e, cmd)
					logger.Info("命令执行结果：", "ip", addr, "命令", cmd, "msg", msg)
				} else {
					msg, err = sendRootMsg(e, cmdMap["centos"])
					//logger.Info("%s centos执行 %s", addr, cmdMap["centos"])
					//logger.Info("centos执行：%s", msg)
				}
				//查看是否存在sdb
				//msg, err = sendRootMsg(e, cmd)
				//logger.Info(field)
				if field != "" {
					logger.Info("centos执行：%s", field)

					if strings.Trim(msg, " ") == "0" {
						continue
					}
					break
				}
				if err != nil {
					logger.Error("%s 命令：%s 失败:%s", addr, cmd, err.Error())
					return errors.New(fmt.Sprintf("%s 命令：%s 失败:%s", addr, cmd, err.Error()))
				}
			}
		}

	}
	time.Sleep(1000)
	logger.Info("执行成功:", "addr", addr, "cmdDescription", cmdDescription)
	//UpdateServerInfo(addr, msg)
	////验证是否加入wheel
	//_, err = sendUserMsg(e, "cat /etc/group |grep wheel|grep JMadmin |wc -l")
	return nil
}
